package cn.idanda.web.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;

import cn.idanda.core.service.account.UserManager;
import cn.idanda.core.service.custom.AccountManager;

/**
 * 
 * @author rain
 * @since 2015年9月13日 下午10:34:01
 */
@Controller
@RequestMapping("user")
public class AccountController {
	@Autowired
	private AccountManager accountManager;
	@Autowired
	private UserManager userManager;
	
	@RequestMapping("logininput")
	public String userLoginInput(){
		return "redirect:base/login";
	}
	
	@RequestMapping("login")
	public String userLogin(@RequestParam(value = "username") String username,
			@RequestParam(value = "password") String password){
		if(accountManager.verifyUser(username, password)){
			return "index";
		}else{
			return "base/login";
		}
	}
	
	@RequestMapping(value = "name" ,method = RequestMethod.POST)
	@ResponseBody
	public String showUserName(@RequestParam(value = "username")String username ){
		try{
			return new Gson().toJson(userManager.getUserName(username));
		}catch(Exception e){
			Map<String,Object> result = new HashMap<String,Object>();
			result.put("failed", "没有该用户!");
			return new Gson().toJson(result);
		}
	}
	
	
	@RequestMapping(value = "allusers" ,method = RequestMethod.GET)
	@ResponseBody
	public String showUsers(HttpServletResponse response){
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("records", userManager.getAllUser());
		return new Gson().toJson(result);
	}
}
